from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

#1.获取数据
iris = load_iris()

#2.数据基本处理
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=22)

#3.特征工程；标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

#4.机器学习
estimator = KNeighborsClassifier(n_neighbors=9)
estimator.fit(x_train, y_train)

#5.模型评估
#方法1，对比真实值和预测值
y_predict = estimator.predict(x_test)
print("预测结果为:\n", y_predict)
print("对比真实值和预测值:\n", y_predict == y_test)#"=="指的是比较两个值是否相等，不相等则会输出false
